package topcoder.srm583;

public class SwappingDigits {
	public String minNumber(String num)
	{
		StringBuilder num_sb = new StringBuilder(num);
		
		
		
		int r_min = num_sb.charAt(num_sb.length() - 1) - '0';
		int r_index = num_sb.length() - 1;
		
		int l_max = num_sb.charAt(0) - '0';
		int l_index = 0;
		
		for(int i = num_sb.length() - 1; i >= 0; i--) {
			int s = num_sb.charAt(i) - '0';
			if(s < l_max) {
				r_min = s;
				r_index = i;
			}
		}
		
		for(int i = 0; i < num_sb.length(); i++) {
			int t = num_sb.charAt(i) - '0';
			if(t > r_min) {
				l_max = t;
				l_index = i;
			}
		}
		
		System.out.println(r_min);
		
		
		char c = num_sb.charAt(l_index);
		num_sb.setCharAt(l_index, num_sb.charAt(r_index));
		num_sb.setCharAt(r_index, c);
		
		
		
		if(num.compareTo(num_sb.toString()) <0 )
			return num;
		else
			return num_sb.toString();
		
	}
	public static void main(String[] args) {
		SwappingDigits sd = new SwappingDigits();
		System.out.println(sd.minNumber("93218910471211292416"));
		
	}

}
